#!/usr/bin/env python

# GPL license 
# created by Taesoo Kwon (taesoobear at gmail dot com)
import pygtk
pygtk.require('2.0')
import gtk, sys, cairo, pango, gobject, string
from math import pi

def draw_rounded(context,area,r = 10):
	#"Draw a rounded rectangle"
	#   A****BQ
	#  H      C
	#  *      *
	#  G      D
	#   F****E
	x,y,w,h=area
	print(x,y,w,h)
	context.move_to(x+r,y)                      # Move to A
	context.line_to(x+w-r,y)                    # Straight line to B
	context.curve_to(x+w,y,x+w,y,x+w,y+r)       # Curve to C, Control points are both at Q
	context.line_to(x+w,y+h-r)                  # Move to D
	context.curve_to(x+w,y+h,x+w,y+h,x+w-r,y+h) # Curve to E
	context.line_to(x+r,y+h)                    # Line to F
	context.curve_to(x,y+h,x,y+h,x,y+h-r)       # Curve to G
	context.line_to(x,y+r)                      # Line to H
	context.curve_to(x,y,x,y,x+r,y)             # Curve to A

class HelloWorld:

	# expose cairo
	def expose (self, widget, event):
		cr = widget.window.cairo_create()

		# Sets the operator to clear which deletes everything below where an object is drawn
		cr.set_operator(cairo.OPERATOR_CLEAR)
		# Makes the mask fill the entire window
		cr.rectangle(0.0, 0.0, *widget.get_size())
		# Deletes everything in the window (since the compositing operator is clear and mask fills the entire window
		cr.fill()
		# Set the compositing operator back to the default
		cr.set_operator(cairo.OPERATOR_OVER)

		# Draw a fancy little circle for demonstration purpose
		cr.set_source_rgba(0.2,0.2,0.2,0.9)
		#cr.arc(widget.get_size()[0]/2,widget.get_size()[1]/2, widget.get_size()[0]/2,0,pi*2)
		#cr.fill()
		draw_rounded(cr, (0,0,widget.get_size()[0], widget.get_size()[1]), 15)
		cr.fill()

		cr.set_source_rgba(1.0,1.0,1.0,1.0)
		x,y,w,h=widget.allocation

		# f=open(self.scriptPath+'mstiler_config.lua','r')
		# config=f.read()
		# f.close()
		# # lua to python conversion
		# config=string.replace(config,"--","#")
		# config=string.replace(config,"{","(")
		# config=string.replace(config,"}",")")
		# config=string.replace(config,"\t","")

		# exec(compile(config,"config.py",'exec'))
		# 
		# tilingMode=tiling_mode[self.currWorkspace]
	
		# msg=""
		# if tilingMode=='v':
		# 	msg="Vertical split mode"
		# elif tilingMode=='f':
		# 	msg="Fullscreen mode"
		# else:
		# 	msg="Horizontal split mode"

		layout=self.layout
		cr.move_to((w-self.fontw-4), (h-self.fonth))
		cr.update_layout(layout)
		cr.show_layout(layout)

	def delete_event(self, widget, event, data=None):
		# If you return FALSE in the "delete_event" signal handler,
		# GTK will emit the "destroy" signal. Returning TRUE means
		# you don't want the window to be destroyed.
		# This is useful for popping up 'are you sure you want to quit?'
		# type dialogs.

		# Change FALSE to TRUE and the main window will not be destroyed
		# with a "delete_event".
		return False

	def destroy(self, widget, data=None):
		gtk.main_quit()

	def timeout(self):
		self.timeOutCount=self.timeOutCount+1
		#x,y,w,h=self.window.allocation
		#self.window.window.invalidate_rect((0,0,w,h),False)
		if self.timeOutCount==7 : gtk.Widget.destroy(self.window)
		return True

	def __init__(self):
		# create a new window
		self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
		self.window.set_decorated(False)

		# Makes the self.windowdow paintable, so we can draw directly on it
		self.window.set_app_paintable(True)
		self.window.set_position(gtk.WIN_POS_CENTER)
		self.window.set_skip_taskbar_hint(True)
		self.window.set_accept_focus(False)

		# This sets the self.windowdows colormap, so it supports transparency.
		# This will only work if the wm support alpha channel
		screen = self.window.get_screen()
		rgba = screen.get_rgba_colormap()
		self.window.set_colormap(rgba)
		self.window.connect('expose-event', self.expose)


		# When the window is given the "delete_event" signal (this is given
		# by the window manager, usually by the "close" option, or on the
		# titlebar), we ask it to call the delete_event () function
		# as defined above. The data passed to the callback
		# function is NULL and is ignored in the callback function.
		self.window.connect("delete_event", self.delete_event)

		# Here we connect the "destroy" event to a signal handler.  
		# This event occurs when we call gtk_widget_destroy() on the window,
		# or if we return FALSE in the "delete_event" callback.
		self.window.connect("destroy", self.destroy)

		# Sets the border width of the window.
		self.window.set_border_width(20)


	def main(self,argv):
		# All PyGTK applications must have a gtk.main(). Control ends here
		# and waits for an event to occur (like a key press or mouse event).
		if len(argv)>1:

			msg=argv[1]
			layout=self.window.create_pango_layout(msg)
			layout.set_font_description(pango.FontDescription("sans serif 11"))
			self.fontw,self.fonth=layout.get_pixel_size()
			self.layout=layout
			self.timer=gobject.timeout_add(100, self.timeout)
			self.timeOutCount=0

			# and the window
			self.window.set_size_request(max(250,self.fontw+40), 30)
			self.window.show()
			gtk.main()
		else:
			print('python mstiler_notify.py msg')

# If the program is run directly or passed as an argument to the python
# interpreter then create a HelloWorld instance and show it
if __name__ == "__main__":
	hello = HelloWorld()
	hello.main(sys.argv)

